#!/usr/bin/perl
# SCRIPT PAGE
use strict;
use warnings;
require 'forum_common.pl';

connect_to_database() or bad_deal();
my $user = get_current_user(get_sess_id());

record_this_hit();

my $post_id = url_param('id');
bad_deal('No post specified!') unless (defined $post_id);


my $post;
bad_deal() unless ($post = get_post($post_id) and
				!($forum::errmsg = cant_editpost($user, $post)));

my $board_title = escapeHTML($post->{B_TITLE});
my $topic_title = escapeHTML($post->{T_TITLE});


my ($msg, $content) = ('');
if (defined($content = param('content')) && $ENV{'REQUEST_METHOD'} eq 'POST')
{
	my ($submit, $post_id);
	if (defined($submit = param('submit')) && $submit eq $forum::preview_text)
	{
		my @errs;
		$msg = forum_error('',join br(),@errs) if @errs=verify_post($content);
		$msg .= br().div({-id=>'post_preview'}, as_post($content));
	}
	elsif (edit_post($post, $user, $content))
	{
		print redirect(-status=>303, -location=>"topic.cgi?id=$post->{TOPIC}");
		exit;
	}
	else
	{
		$msg = nl2br(forum_error());
	}
}

print
	header($forum::http_header), forum_start(forum_cpanel($user)),
	"Board: $board_title", br(),
	"Topic: $topic_title", br(),
	br(),
	$msg,
	start_form(-action=>'', -method=>'post'),
	'<fieldset id="makepost">',
	legend('New Post'),
	'Post Content: (max 15000 characters)', br(),
	textarea(-name=>'content', -cols=>100, -rows=>20, -id=>'postcontent',
			-value=>($content || $post->{CONTENT})),
	div({-class=>'subbtn'},
		submit(-name=>'submit', -value=>'Preview'),
		submit(-name=>'submit', -value=>'Edit')),
	'</fieldset>',
	end_form();


gameover:
print forum_end();
forum_disconnect();
